<template>
  <div id="app">
    <el-card shadow="always">
      <el-form :inline="true" :model="findResult" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="findResult.sname" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="findResult.sex" placeholder="性别"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="findResult.addr" placeholder="年龄"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="findResult.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="StudentList"
          style="width: 100%">
        <el-table-column
            label="学号"
            align="center"
            prop="sid">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="sname">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="地址"
            align="center"
            prop="address">
        </el-table-column>
        <el-table-column
            label="班级"
            align="center"
            prop="cid">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 导航 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import studentApi from "@/api/student";
import classApi from "@/api/class"

export default {
  data() {
    return {
      StudentList: [], // 存储学生数据
      page: 1, // 当前页 默认1
      pageSize: 10, // 默认每页显示10条
      total: 0, // 总记录数 默认0
      findResult: {}, //查询条件对象，初始化为空对象
      classList: [] // 存储班级列表
    };
  },
  created() {
    // 页面创建时默认加载数据
    this.findByPage(this.page, this.pageSize);
    this.findClass();
  },
  methods: {
    // 查找所有学生
    async findStudents() {
      const { data: res } = await studentApi.findAllStudents();
      if (res.code === 200) {
        this.StudentList = res.data.list;
      }
    },
    // 点击查询按钮触发查询，包括分页和条件
    async onSubmit() {
      const { data: res } = await studentApi.searchStudent(this.page, this.pageSize, this.findResult);
      console.log(res)
      if (res.code === 200) {
        this.StudentList = res.data.pageResult.rows; // 获取学生数据
        this.total = res.data.pageResult.total; // 获取总条数
      }
    },
    // 分页查询
    async findByPage(page, pageSize, findResult = {}) {
      const { data: res } = await studentApi.findByPage(page, pageSize, findResult);
      if (res.code === 200) {
        this.StudentList = res.data.pageResult.rows; // 获取学生数据
        this.total = res.data.pageResult.total; // 获取总条数
      }
    },
    // 查找班级
    async findClass() {
      const { data: res } = await classApi.findAllClass();
      if (res.code === 200) {
        this.classList = res.data.list; // 获取班级数据，确保有默认值
      }
    },
    // 改变每页显示的条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.findByPage(this.page, this.pageSize, this.findResult); // 重新加载数据
    },

    // 改变当前页
    handleCurrentChange(val) {
      this.page = val; // 改变当前页
      this.findByPage(this.page, this.pageSize, this.findResult); // 重新加载数据
    },

    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row);
      // 添加编辑逻辑
    },

    // 删除操作
    async handleDelete(index, row) {
      try {
        const { data: res } = await studentApi.deleteStudent(row.sid);
        if (res.code === 200) {
          this.StudentList.splice(index, 1); // 删除当前行
          this.total -= 1; // 更新总条数
        }
      } catch (error) {
        console.error("删除失败", error);
      }
    }
  }
};

</script>

<style>

</style>
